package com.franklin.ideaplugin.maphelper.col.declaration;

import com.franklin.ideaplugin.api.beans.declaration.IDeclaration;
import com.franklin.ideaplugin.api.beans.declaration.impl.DeclarationStatementDeclaration;
import com.franklin.ideaplugin.api.beans.declaration.impl.FieldDeclaration;
import com.franklin.ideaplugin.api.beans.declaration.impl.MethodCallExpressionDeclaration;
import com.franklin.ideaplugin.maphelper.col.declaration.impl.DeclarationStatementColDeclaration;
import com.franklin.ideaplugin.maphelper.col.declaration.impl.FieldColDeclaration;
import com.franklin.ideaplugin.maphelper.col.declaration.impl.MethodCallExpressionColDeclaration;
import com.google.common.collect.Maps;
import com.intellij.openapi.application.ApplicationManager;

import java.util.Map;
import java.util.function.Function;

/**
 * @author Ye Junhui
 * @since 2024/3/6
 */
public class ColDeclarationFactory {

    private final Map<Class<? extends IDeclaration>, Function<IDeclaration, IColDeclaration>> factory = Maps.newHashMap();

    public static ColDeclarationFactory getInstance(){
        return ApplicationManager.getApplication().getService(ColDeclarationFactory.class);
    }

    public ColDeclarationFactory() {
        factory.put(DeclarationStatementDeclaration.class,declaration -> new DeclarationStatementColDeclaration((DeclarationStatementDeclaration) declaration));
        factory.put(FieldDeclaration.class, declaration -> new FieldColDeclaration((FieldDeclaration) declaration));
        factory.put(MethodCallExpressionDeclaration.class, declaration -> new MethodCallExpressionColDeclaration((MethodCallExpressionDeclaration) declaration));
    }

    /**
     * 创建map定义
     * @param declaration
     * @return
     */
    public IColDeclaration create(IDeclaration declaration){
        Function<IDeclaration, IColDeclaration> mapper = factory.get(declaration.getClass());
        return mapper.apply(declaration);
    }
}
